﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Movie.web
{
    public partial class SeatSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataList1.RepeatColumns = 7;
                int id =Convert.ToInt32( Request["ScheduleID"]);
                DataList1.DataSource = BLL.SeatBLL.GetSeat(id);
                DataList1.DataBind();
            }

        }

        
        public string GetPicoath(object ss1)
        {
            Modal.SeatState ss2 = (Modal.SeatState)ss1;

            if (ss2==Modal.SeatState.SELECT)
            {
                return "~/images/covers/select.jpg";
            }
            else if (ss2==Modal.SeatState.UNSELECT)
	        {
                return "~/images/covers/unselect.jpg";
	        }
             else
	        {
                return "~/images/covers/yes.jpg";
	        }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request["ScheduleID"]);
            //实例化这两个对象是为了order的初始化
            Modal.Movies movie = BLL.MovieBLL.GetMovieByScheduleId(id);
            Modal.User user = Session["UserInfo"] as Modal.User;
           
            Modal.Order order = new Modal.Order//(初始化器)
            {
                ScheduleID=id,
                Mname=movie.Mname,
                Mpicture=movie.Mpicture,
                Price=movie.Mprice,
                CustID= user.CustID,
                Ooff=false,
                OPaid=true,
                Orevoke=false,
                OrderDate=DateTime.Now

            };

            List<Modal.OrderSeat> orderSeat = new List<Modal.OrderSeat>();

            for (int i = 0; i < DataList1.Items.Count ; i++)
            {
                ImageButton imgbtn = DataList1.Items[i].FindControl("ImageButton1") as ImageButton;

                if (imgbtn.ImageUrl == "~/images/covers/selected.jpg")
                {
                    Modal.OrderSeat seat = new Modal.OrderSeat();
                    //初始化器
                    //{
                    //    SeatID = (DataList1.Items[i].DataItem as Modal.Seat).SeatID
                    //};
                    seat.SeatID=Convert.ToInt32(imgbtn.CommandArgument);

                   orderSeat.Add(seat);
                   
                }

            }
            order.Num = orderSeat.Count;
            
            BLL.OrderBLL.InsertOrder(order);
        }

        protected void ImageButton1_Command(object sender, CommandEventArgs e)
        {                       
            ImageButton imgbtn = sender as ImageButton;
            imgbtn.ImageUrl = "~/images/covers/selected.jpg";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/OrderList.aspx");
        }


    }
}